using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SqlServer.Management.Smo;

namespace DataSetGeneration
{
    internal class TableWrapper
    {
        private Table _table;
        private Column _primaryKeyColumn;
        private List<Column> _nonPrimaryKeyColumns;
        private string _prettyName;

        internal TableWrapper(Table table)
        {
            _table = table;

            SetPrimaryKeyColumn();
            SetNonPrimaryKeyColumns();
            SetPrettyTableName();
        }

        public Table Table
        {
            get { return _table; }
        }

        public string PrettyTableName
        {
            get { return _prettyName; }
        }

        public Column PrimaryKeyColumn
        {
            get { return _primaryKeyColumn; }
        }

        public List<Column> NonPrimaryKeyColumns
        {
            get { return _nonPrimaryKeyColumns; }
        }

        private void SetPrimaryKeyColumn()
        {
            foreach (Column c in _table.Columns)
            {
                if (c.InPrimaryKey && !c.IsForeignKey)
                {
                    _primaryKeyColumn = c;
                    return;
                }
            }

            throw new ArgumentException("Table does not have a primary key");
        }

        private void SetNonPrimaryKeyColumns()
        {
            _nonPrimaryKeyColumns = new List<Column>();

            foreach (Column c in _table.Columns)
            {
                if (!c.InPrimaryKey)
                    _nonPrimaryKeyColumns.Add(c);
            }
        }

        private void SetPrettyTableName()
        {
            _prettyName = Utils.GetPrettyObjectName(_table.Name);
        } 
    }
}
